package typejump.commands;

import typejump.AnimationManager;
import typejump.Messenger;
import typejump.Settings;
import typejump.Swagger;
import typejump.TypeJump;
import typejump.engine.GameEngine;
import typejump.engine.OnGoingThing;
import typejump.objects.Actor;
import typejump.objects.AttachableSparkles;
import typejump.objects.GameObject;
import typejump.objects.Sparkles;
import typejump.objects.TextEffect;

public class Hover implements TextCommand, OnGoingThing {

	private Sparkles sparkles;
	private double swagPerSecond = Settings.getDouble("hover.cost");
	private double hoverStartY = 0;
	
	@Override
	public boolean matches(String command) {
		return command.equalsIgnoreCase("hover");
	}

	private TextEffect effect;
	
	@Override
	public void act(String command) {
		TypeJump tj = TypeJump.getInst();
		
		Actor player = tj.getPlayer();
		hoverStartY = player.bottom();
		
		
		GameEngine.addOnGoingThing(this);
		
		effect = GameEngine.addTextEffect("HOVER", "hover");
		
		if (sparkles != null)
			sparkles.stopSparkling();
		
		sparkles = new AttachableSparkles(GameEngine.getPlayer(), AnimationManager.SPARKLE_HOVER, 30);
		sparkles.setScale(0.25);
		sparkles.setSpeedX(0, 20);
		sparkles.setVelRuleY(Sparkles.VEL_ALLOW_POS);
		GameEngine.addEndEvent(this, sparkles, "stopSparkling");
		
		GameEngine.addEffect(sparkles);
	}

	@Override
	public boolean happen(TypeJump tj, double seconds) {
		Actor p = tj.getPlayer();
		Swagger swag = tj.getSwagMeter();
		
		if (!swag.tryExpendSwag(swagPerSecond * seconds)
				|| Messenger.hasMessage("fall") || p.isOnGround()) {
			return true;
		}
		
		if (p.bottom() > hoverStartY)
			p.bottom(hoverStartY);
		
		p.vy(0);
		GameEngine.blockSwagRegen();
		
		return false;
	}

}
